package gam.t1;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import gam.t1.conf.TextConfig;
import gam.t1.triggers.HoverTrigger;
import gam.t1.triggers.TestTrigger;
import gam.t1.triggers.URLTrigger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.ChatColor;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public final class TextParser {
    private static Map<String,TestTrigger> triggers = Maps.newHashMap();
    static {
        triggers.put("url",new URLTrigger());
        triggers.put("hover",new HoverTrigger());
    }
    public static Component parser(String var) throws Exception {
        Objects.requireNonNull(var);
        Component var1 = Component.empty();
        if (var.contains("{") && var.indexOf("{") != 0 ){
            var1 = var1.append(toTranslate(var.substring(0,var.indexOf("{"))));
        }
        List<String$TextConponent> var2 = translate(var);
        if (var2.size() > 0){
            for (String$TextConponent var3 : var2) {
                Component var4 = toTranslate(var3.origin());
                Map<String, String> entry = var3.getEntry();
                for (String type : entry.keySet()) {
                    String value = entry.get(type);
                    TestTrigger handler = triggers.get(type);
                    if (handler != null){
                        var4 = handler.runTrigger(var4,value);
                    }
                }
                var1 = var1.append(var4);
            }
        } else var1 = var1.append(toTranslate(var));

        return var1;
    }
    private static boolean a(String b){
        return "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx".contains(b);
    }
    private static TextComponent b(String b){
        char[] c = b.toCharArray();
        int d = 0;
        for (int i = 0; i < c.length - 1; i++) {
            if (!a(String.valueOf(c[i]))){
                break;
            } else {
                d++;
            }
        }
        if (d == 0) return Component.text(b);
        b = b.substring(d);
        TextComponent text = null;
        for (int i = 0; i < d; i++) {
            if (text == null){
                text = Component.text("\u00A7" + c[i] + b);
            } else {
                text = text.content("\u00A7" + c[i] + text.content());
            }
        }
        return text;
    }
    public static Component toTranslate(String t){
        Objects.requireNonNull(t);
        Component c = Component.empty();
        String[] var1 = t.split(TextConfig.prefix);
        System.out.println(Arrays.toString(var1));
        if (var1.length > 1){
            for (int i = 0; i < var1.length; i++) {
                if (var1[i].length() == 1 && a(var1[i])){
                    if (i + 1 < var1.length){
                        var1[i + 1] = var1[i] + var1[i + 1];
                    }
                    var1[i] = "";
                }
                if (!var1[i].equals("")){
                    c = c.append(b(var1[i]));
                }
            }
        } else if (var1.length > 0){
            c = c.append(b(var1[0]));
        }
        return c;
//        return t.replaceAll("&","");
    }
    private static List<String$TextConponent> translate(String origin) throws Exception {
        Objects.requireNonNull(origin);
        List<String$TextConponent> list = Lists.newArrayList();
        CharacterIterator it = new StringCharacterIterator(origin);
        int a = -1,b = -1,e;
        String prefix = null;
        String suffix = null;
        while (it.current() != CharacterIterator.DONE){
            char c = it.current();
            if (a == -1 && c == '{'){
                a = it.getIndex();
            } else if (b == -1 && c == '}'){
                b = it.getIndex() + 1;
            }
            if (a != -1 && b != -1 && c == '{'){
                e = it.getIndex();
                String in_prefix = origin.substring(a,b);
                String in_suffix = origin.substring(b,e);
                list.add(new String$TextConponent(in_prefix,in_suffix));
                a = -1;b = -1;e = -1;
                continue;
            }
            it.next();
        }
        if (a != -1 && b != -1){
            prefix = origin.substring(a,b);
            suffix = origin.substring(b);
        }
        if (prefix != null && suffix != null){
            list.add(new String$TextConponent(prefix,suffix));
        }
        return list;
    }

    private static String trim(String v){
        return v.trim();
    }

    private static class String$TextConponent{
        private final String option; // 代表{...}
        private final String origin; // 代表{}字符后的字符
        static String$TextConponent newIns(String a, String b){return new String$TextConponent(a,b);}
        private String$TextConponent(String option, String origin) {
            this.option = option;
            this.origin = origin;
        }

        public Map<String,String> getEntry(){
            Map<String,String> t = Maps.newHashMap();
            String _option = option();
            if (_option.contains("|")){
                // {url:...,hover: ...}
                for (String var : _option.split("\\|")) {
                    if (var.contains("-")){
                        String[] var_1 = var.split("-");
                        t.put(trim(var_1[0]),trim(var_1[1]));
                    }
                }
            } else {
                if (_option.contains("-")){
                    String[] var_1 = _option.split("-");
                    t.put(trim(var_1[0]),trim(var_1[1]));
                }
            }
            return t;
        }

        public String option(){
            return trim(option.replace("{","").replace("}",""));
        }
        public String origin(){
            return trim(origin);
        }
    }
}
